class_name RaceSystem
extends BaseSystem

#region 属性
@export var race_infos:Array[RaceInfo]
var race_info_dict:Dictionary
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init_system(args = null):
	super(args)
	race_info_dict = {}
	for info in race_infos:
		if not race_info_dict.has(info.id):
			race_info_dict[info.id] = info
#endregion
#region 公共方法
func get_original_race_id_by_race(race:GameEnum.OriginalRace) -> String:
	var res = race_info_dict.values().filter(func(info:RaceInfo):return info is OriginalRaceInfo and info.race == race)
	return res[0].id if res.size() > 0 else ""
func get_variant_race_id_by_race(race:GameEnum.OriginalRace) -> String:
	var res = race_info_dict.values().filter(func(info:RaceInfo):return info is VariantRace and info.race == race)
	return res[0].id if res.size() > 0 else ""
func get_target(race_ids:Array[String],pos:Vector2 = Vector2.ZERO,is_sort:bool = false,count:int = -1,radius:float = 0) -> Array[Node]:
	var targets:Array[Node] = []
	for race_id in race_ids:
		var race_info = get_race_info(race_id)
		if not race_info:
			continue
		var group_name:String = "Race_" + race_info.id
		if get_tree().has_group(group_name):
			for node in get_tree().get_nodes_in_group(group_name):
				if not targets.has(node):
					targets.append(node)
	if targets.size() > 0:
		if is_sort:
			targets.sort_custom(func (a,b):return (a.global_position - pos).length() < (b.global_position - pos).length())
		if radius > 0:
			targets = targets.filter(func (x):return (x.global_position - pos).length() <= radius)
		count = targets.size() if count < 0 else count
		return targets.slice(0,count)
	else:
		return []
func group_add(race_id:String, item:Node):
	var race_info = get_race_info(race_id)
	if not race_info:
		return
	var group_name:String = "Race_" + race_info.id
	if not item.is_in_group(group_name):
		item.add_to_group(group_name)
	for id in race_info.get_original_race_ids():
		group_add(id,item)
func group_remove(race_id:String,item:Node):
	var race_info = get_race_info(race_id)
	if not race_info:
		return
	var group_name:String = "Race_" + race_info.id
	if item.is_in_group(group_name):
		item.remove_from_group(group_name)
	for id in race_info.get_original_race_ids():
		group_remove(id,item)
func get_race_info(race_id:String) -> RaceInfo:
	return race_info_dict.get(race_id)
func get_target_race_infos(race_id:String) -> Array[RaceInfo]:
	if race_info_dict.has(race_id):
		var target_race_infos:Array[RaceInfo] = []
		for id in get_race_info(race_id).target_race_ids:
			var race_info = get_race_info(id)
			if race_info:
				target_race_infos.append(race_info)
		return target_race_infos
	return []
func get_friend_race_infos(race_id:String) -> Array[RaceInfo]:
	if race_info_dict.has(race_id):
		var target_race_infos:Array[RaceInfo] = []
		for id in get_race_info(race_id).friend_race_ids:
			var race_info = get_race_info(id)
			if race_info:
				target_race_infos.append(race_info)
		return target_race_infos
	return []
#endregion
#region 私有方法
#endregion
#region 生命周期函数
#endregion
